﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using BO.Custom;
using Business;
using Entity;
using FreshCms.BackEnd.ControlsCommon;
using Utilities;
using System.Text.RegularExpressions;

namespace FreshCms.BackEnd.Controls.New
{
    public partial class NewRecycle : System.Web.UI.UserControl
    {
        #region check login and permission
        InPms chkPms = new InPms();
        public Int32 panelId = 0;
        private void checkLogin()
        {
            // Check and load UC
            if (Request.QueryString["ui"] != null)
                Int32.TryParse(Request.QueryString["ui"], out panelId);
            // check Pms
            if (panelId > 0)
                chkPms = InPmsBusiness.CheckSessionPms(panelId);
            if (chkPms == null)
                Response.Redirect("/BackEnd/xlogin.aspx?redirect=" + Utils.GetCurrentCmsPage().Replace("?", "@").Replace("&", "|"));
            // show Pms in Top Bar
            TopBarPms ucTopBar = Page.Master.FindControl("TopBarPms") as TopBarPms;
            ucTopBar.BindData(chkPms);
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            checkLogin();
            if (!Page.IsPostBack)
            {
                if (chkPms.PmsView == 1)
                {
                    BindCate();
                    BindData();
                }
            }
        }

        private void BindCate()
        {
            List<ListItem> lst = new List<ListItem>();
            lst.Add(new ListItem(" -- Tất cả danh mục --", "0"));

            var lstParent = OutCategoryBusiness.GetOutCategoryList(1, 1);
            foreach (OutCategory parent in lstParent)
            {
                lst.Add(new ListItem(parent.CatName, parent.CatId.ToString() + "|0"));
                var lstChild = OutCategoryBusiness.GetOutCategoryChildList(parent.CatId, 1);
                foreach (OutCategory child in lstChild)
                {
                    lst.Add(new ListItem("  ----- " + child.CatName, child.CatId.ToString()));
                }
            }

            ddlCate.Items.AddRange(lst.ToArray());

            // Bind color 
            for (int i = 1; i <= ddlCate.Items.Count - 1; i++)
            {
                try
                {
                    int.Parse(ddlCate.Items[i].Value);
                }
                catch (Exception ex)
                {
                    ddlCate.Items[i].Attributes.Add("style", "color:red;background-color:LightGray;");
                }
            }
        }

        public Int32 page = 1;
        public Int32 size = 10;
        public string title = "";
        public string strCate = "";
        public Int32 stt = -1;
        public Int32 cate = -1;
        public string from, to = string.Empty;

        private void BindData()
        {
            page = Utils.GetQuery("page", 1);
            title = Utils.GetQuery("title", "");
            from = Utils.GetQuery("from", "");
            to = Utils.GetQuery("to", "");

            // get cate
            bool isParent = false;
            if (!string.IsNullOrEmpty(Request.QueryString["cate"]))
            {
                strCate = Request.QueryString["cate"];

                string[] temp = strCate.Split('|');
                if (temp.Length > 1)// split ra dc 2 phan tu thi la PARENT
                {
                    Int32.TryParse(temp[0], out cate);
                    isParent = true;
                }
                else
                {
                    Int32.TryParse(strCate, out cate);
                }
            }
            if (cate < 0) cate = 0;

            // get date
            DateTime dFrom = new DateTime(2011, 1, 1);
            DateTime dTo = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day).AddDays(1);

            if (!(String.IsNullOrEmpty(from)))
                dFrom = DateTime.ParseExact(from, "d/M/yyyy", null);

            if (!(String.IsNullOrEmpty(to)))
                dTo = DateTime.ParseExact(to, "d/M/yyyy", null).AddDays(1);

            Int32 total = 0;
            var lst = OutNewBusiness.GetOutNewListByPaging(isParent, title, dFrom, dTo, cate, stt, page, size, ref total);
            if (lst.Count > 0)
            {
                rpt.DataSource = lst;
                //rpt.ItemDataBound += new RepeaterItemEventHandler(rpt_ItemDataBound);
                rpt.DataBind();
            }
            if (total > size)
            {
                Pager.Index = page;
                Pager.Cate = Regex.Replace(Utils.GetCurrentCmsPage(), "&page=(.*)", "");
            }
            else
                Pager.Visible = false;

            // Bind value to form 
            ddlCate.SelectedValue = strCate;
        }
    }
}